/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-12-17 14:47:51
 * @filename : controller/aol_spam_overview.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){
        
    var name = 'AOL Spam Overview', mId = 'aol_spam_overview',
        tpl = require('{view}page_email.tpl'),
        tplH = require('{view}page_email_head.tpl'),
        tplD = require('{view}page_email_desc.tpl'),
        tplT = require('{view}table.tpl'),
        tplS = require('{view}select.tpl');
      
    var tempOpt = {
             engine : '',
             begin_date : null,
             end_date : null,
        };
 
    var AOLSpamOView = Backbone.View.extend({
    
        initialize: function (obj) { 
            this.run(obj);
        },

        run : function(obj) {
            if ($('.main').length !== 3) { 
                Backbone.routerObj.navigate('index/w/'+mId, { trigger: true });return;
            } else {
                this.tplRender();
            }
        },

        tplRender : function() {
            if ($('#'+mId).length < 1) { 
                $('#index_right').append(_.template(tpl)({obj:{name:name,mId:mId}}));
                $('#'+mId).find('.m_head').append(_.template(tplH));
                this.defaultOpt();
                this.afterBindEvt();
                this.fetchData(tempOpt); 
            } else {
                $('#'+mId).css('display','block');
            } 
        },

        afterBindEvt : function() {
             var tthis = this,dom = $('#'+mId);
             
             new Pikaday({
                    field: dom.find('.from_time')[0],
                    firstDay: 1,
                    minDate: new Date('1997-01-01'),
                    maxDate: new Date('2020-12-31'),
                    yearRange: [1997,2020]
             });
             
             new Pikaday({
                    field: dom.find('.to_time')[0],
                    firstDay: 1,
                    minDate: new Date('1997-01-01'),
                    maxDate: new Date('2020-12-31'),
                    yearRange: [1997,2020]
             });

            dom.find('.selectTime').on('change',function(){
                var val = $(this).val();
                    share.setInputDate(val,mId);
            });


            dom.find('.do').on('click',function(){
                var obj = tthis.getPostData(); 
                if (obj) { 
                    tthis.fetchData(obj);
                }
            });

        },
     
        defaultOpt : function() {
             var dom = $('#'+mId);
             this.selectSite();
             share.setInputDate('last-week',mId);
             tempOpt.end_date = dom.find('.to_time').val();
             tempOpt.begin_date = dom.find('.from_time').val(); 
             tempOpt.engine  = '';
        },
 
        selectSite : function() {
             var siteInfo =  share.getAllSites(1);
             $('#'+mId).find('.selectInfo').html(_.template(tplS)({obj:siteInfo}));
        }, 

        getPostData : function() {
             var dom = $('#'+mId);
             tempOpt.engine = dom.find('.selectInfo').find('select').val();
             if (tempOpt.engine == '0') { tempOpt.engine = ''};
             tempOpt.end_date = dom.find('.to_time').val();
             tempOpt.begin_date = dom.find('.from_time').val();

             if (tempOpt.end_date == '' || tempOpt.begin_date == '') {
                 alert('The time is required!');
                 return false;
             }
             if (new Date(tempOpt.end_date) < new Date(tempOpt.begin_date)) {
                 alert('The end time is less than the start time!');
                 return false;
             }
             if (new Date(tempOpt.end_date) > new Date()) {
                 alert('The end time is greater than the current time!');
                 return false;
             }
             return tempOpt;
        }, 

        fetchData : function(data) {
            var tthis = this,
                ajaxOpt = {
                url : seajs.data.vars.apiUrl+'email_status',
                type : 'POST',
                data : data,
                success : function(data) {                  
                    tthis.fetchSuccess(data);
                },
                error : function(xhr, errorType, error) {
                },
                complete : function(xhr,status){
                    share.load($('#'+mId),false); 
                }
            };
           
            share.load($('#'+mId),true); 
            share.ajax(ajaxOpt,true);
        },

        fetchSuccess : function(data) {
            console.log(data);
            this.details(data);
        },

        details : function(data) {
            var tthis = this,
                arr = [];

            arr.push({name:'Project',val:'NAME'});
            arr.push({name:'Number of report users',val:'USER_CNT'});
            arr.push({name:'Number of messages',val:'MESSAGE_CNT'});

            var total = {};
                total.BEING_DATE = tempOpt.begin_date;
                total.END_DATE = tempOpt.end_date;
                total.SITE = share.getSiteName(tempOpt.engine);
                total.MES_TATOL = 0;
                total.USER_TATOL = 0;

            var siteNum = Number(tempOpt.engine);
            if (siteNum > 0) {
                var site = share.getSiteName(siteNum);
                data=_.where(data,{NAME:site});
            }

            _.each(data,function(obj) {
                total.MES_TATOL += Number(obj.MESSAGE_CNT);
                total.USER_TATOL += Number(obj.USER_CNT);
            }); 

            $('#'+mId).find('.table').html(_.template(tplT)({obj:data,arr:arr}));
            $('#'+mId).find('.desc').html(_.template(tplD)({obj:total}));
        }

    });
	module.exports = AOLSpamOView;
});


